package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.dao.RentalContractDao;
import com.tianqu.tms.dao.RentalLocationInfoDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.ViewListDao;
import com.tianqu.tms.dto.RentalInfoDto;

public class RentalLocationInfo  extends ViewListManager  {

	private static final long serialVersionUID = 1L;
	private int rowNo;
	private long id;
	private String locationSerial;//租位编号
	private int rentalType;//租位类型
	private String rentalTypeName;//租位类型名
	private int acreage;//租位面积
	private int carPort;//车位数量
	private int area;//租位区域
	private String rentalStatusName;//租位状态名
	private Timestamp insertTime;//插入时间
	private Timestamp updateTime;//更新时间
	private int insertUserId;//插入用户ID	
	private int updateUserId;//更新用户ID
	private String insertUserName;//插入用户名
	private String updateUserName;//更新用户名
	private int cssFlag;// 样式标识
	private String address;//租位位置
	private int handleType;//操作类型
	private String remark;//备注
	private String handleTypeName;//操作类型
	private String handleName;//操作人姓名
	private String handleDate;//操作日期
	private String annualTransactionVolume;//允许库存登记数量
	
	
	public static RentalLocationInfo getInstance() {
		RentalLocationInfo rentalLocationInfo = new RentalLocationInfo();
		rentalLocationInfo.setExisting(false);
		return rentalLocationInfo;
	}
	
	public String findRentalLocationInfo(long id) throws TmsDaoException {
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		RentalInfoDto dto = rentalDao.searchLocationInfoById(id);
		String tempStr = "";
		if(dto != null)
		{
			Codes code = Codes.getInstance();
			String rentalType = code.getItemValueByItemIdAndItemCode(Codes.RENTAL_TYPE, dto.getRentalType());
			String area = code.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, dto.getArea());
			tempStr = rentalType + "," + String.valueOf(dto.getAcreage()) + "," + String.valueOf(dto.getCarPort()) + "," + area;		
		}
		return tempStr;		
	}

	/**
	 * 租位编号存在check
	 * @return
	 * @throws TmsDaoException
	 */
	public boolean isExistLocationInfoBySerial(String locationSerial) throws TmsDaoException {
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		return rentalDao.isExistLocationInfoBySerial(locationSerial);
	}
	
	/**
	 * 插入租位信息
	 * @return
	 * @throws TmsDaoException
	 */
	public void insertLocationInfo(RentalLocationInfo info)throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		RentalInfoDto dto = new RentalInfoDto();
		dto = setDtoFromEntity(dto,info);
		rentalDao.insertLocationInfo(dto);
		
	}
	
	/**
	 * 插入租位操作记录
	 * @return
	 * @throws TmsDaoException
	 */
	public void insertRentalUpdateHistroy(RentalLocationInfo info)throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		RentalInfoDto dto = new RentalInfoDto();
		dto = setDtoFromEntity(dto,info);
		rentalDao.insertRentalUpdateHistroy(dto);
		
	}
	
	/**
	 * 
	 * 查询租位一览
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
	public List<RentalLocationInfo> findRentalLocationList(String locationSerial,String[] rentalType,String[] rentalStatus,
			String acreageS,String acreageE,String carPortS,String carPortE,int rentalArea,
			String updateTimeS,String updateTimeE) throws TmsDaoException
	{
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO,
				getPageNo(), getMaxIndex(), getSortKey(), isAsc());
		ViewListDao idao = (ViewListDao) TmsDao.getViewListDao((TmsDao) rentalDao,TmsDao.TMS_RENTALLOCATION_INFO);

		List<RentalInfoDto> list = rentalDao.findRentalLocationList(locationSerial,rentalType,rentalStatus,
				acreageS,acreageE,carPortS,carPortE,rentalArea,updateTimeS,updateTimeE);
		List<RentalLocationInfo> locationInfoList = new ArrayList<RentalLocationInfo>();
		
		if (null != list && list.size() > 0) {
			
			for (int i = 0; i < list.size();i++) {
				RentalLocationInfo rentalLocationInfo= new RentalLocationInfo();
				rentalLocationInfo = setEntityFromDto(list.get(i));
				locationInfoList.add(rentalLocationInfo);
			}
		}
		
		setPagingInfo(idao);
		return locationInfoList;	
	}
	
	
	/**
	 * 查询租位合同信息操作记录
	 */
	public List<RentalLocationInfo> searchRentalLocationUpdateHistroy(String serial,String handleName,
			String handleType,String handleDateS,String handleDateE) throws TmsDaoException {

		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO,
				getPageNo(), getMaxIndex(), getSortKey(), isAsc());
		ViewListDao idao = (ViewListDao) TmsDao.getViewListDao((TmsDao) rentalDao,TmsDao.TMS_RENTALLOCATION_INFO);

		//合同信息list
		List<RentalInfoDto> list = rentalDao.searchRentalLocationUpdateHistroy(serial,handleName,handleType,handleDateS,handleDateE);
		List<RentalLocationInfo> rentalInfoUpdateHistroyList = new ArrayList<RentalLocationInfo>();
		if (null != list && list.size() > 0) {			
			for (int i = 0; i < list.size();i++) {
				RentalLocationInfo rentalLocationInfo= new RentalLocationInfo();
				rentalLocationInfo = setUpdateHistroyDtoTransformInfo(list.get(i));
				rentalInfoUpdateHistroyList.add(rentalLocationInfo);
			}
		}	
		setPagingInfo(idao);
		return rentalInfoUpdateHistroyList;
	}
	
	/**
	 * 查询租位合同信息操作记录
	 */
	public List<RentalLocationInfo> searchRentalLocationUpdateHistroyNopage(String serial,String handleName,
			String handleType,String handleDateS,String handleDateE) throws TmsDaoException {

		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_CONTRACT_INFO);
		
		//合同信息list
		List<RentalInfoDto> list = rentalDao.searchRentalLocationUpdateHistroy(serial,handleName,handleType,handleDateS,handleDateE);
		List<RentalLocationInfo> locationUpdateHistroyList = new ArrayList<RentalLocationInfo>();
		if (null != list && list.size() > 0) {			
			for (int i = 0; i < list.size();i++) {
				RentalLocationInfo rentalLocationInfo= new RentalLocationInfo();
				rentalLocationInfo = setUpdateHistroyDtoTransformInfo(list.get(i));
				locationUpdateHistroyList.add(rentalLocationInfo);
			}
		}	

		return locationUpdateHistroyList;
	}
	
	/**
	 * 
	 * 查询租位信息
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
	public RentalLocationInfo searchLocationDetailBySerial(String serial) throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		RentalInfoDto dto = rentalDao.searchLocationDetailBySerial(serial);

		return setEntityFromDtoInfo(dto);
	}
	
	/**
	 * 
	 * 查询租位详细信息
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
	public RentalLocationInfo searchLocationDetailById(long id) throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		RentalInfoDto dto = rentalDao.searchLocationDetailById(id);

		return setEntityFromDto(dto);
	}
	
	/**
	 * 租位状态check
	 * @return
	 * @throws TmsDaoException
	 */
	public int statusCheckById(long id) throws TmsDaoException {
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		return rentalDao.statusCheckById(id);
	}
	
	/**
	 * 删除租位信息
	 * @return
	 * @throws TmsDaoException
	 */
	public void deleteLocationById(long id)throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		rentalDao.deleteLocationById(id);
	}
	
	/**
	 * 更新租位信息
	 * @return
	 * @throws TmsDaoException
	 */
	public void updateLocationInfo(RentalLocationInfo rentalLocationInfo)throws TmsDaoException {
		
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);
		rentalDao.updateLocationInfo(rentalLocationInfo);
	}
	
	/**
	 * 
	 * 模糊查询租位编号列表
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
	public List<RentalLocationInfo> findLocationSerialList(String query) throws TmsDaoException
	{
		RentalLocationInfoDao rentalDao = (RentalLocationInfoDao) TmsDao.getInstance(TmsDao.TMS_RENTALLOCATION_INFO);

		List<RentalInfoDto> list = rentalDao.findLocationSerialList(query);
		List<RentalLocationInfo> locationInfoList = new ArrayList<RentalLocationInfo>();
		
		if (null != list && list.size() > 0) {
			
			for (int i = 0; i < list.size();i++) {
				RentalLocationInfo rentalLocationInfo= new RentalLocationInfo();
				rentalLocationInfo = setEntityFromDto(list.get(i));
				locationInfoList.add(rentalLocationInfo);
			}
		}

		return locationInfoList;	
	}
	
	/**
	 * 从dto向Entity赋值
	 * @param marketCarInfoDto
	 * @param car
	 * @return
	 */
	public RentalLocationInfo setUpdateHistroyDtoTransformInfo(RentalInfoDto rentalInfoDto) throws TmsDaoException{
		RentalLocationInfo locationInfo = new RentalLocationInfo();
		Codes code = Codes.getInstance();
		locationInfo.setId(rentalInfoDto.getId());
		locationInfo.setLocationSerial(rentalInfoDto.getSerial());
		locationInfo.setHandleTypeName(code.getItemValueByItemIdAndItemCode(Codes.RENTAL_UPDATE_TYPE, rentalInfoDto.getHandleType()));
		locationInfo.setRemark(rentalInfoDto.getRemark());
		locationInfo.setHandleName(rentalInfoDto.getHandleName());
		locationInfo.setHandleDate(DateUtil.getDateByTimeStampHM(rentalInfoDto.getHandleDate()));
		locationInfo.setRowNo(rentalInfoDto.getRowNo());
		locationInfo.setCssFlag(rentalInfoDto.getCssFlag());
		return locationInfo;
	}
	
	/**
	 * 从Entity向dto赋值
	 * @param RentalInfoDto
	 * @param car
	 * @return
	 */
	private RentalInfoDto setDtoFromEntity(RentalInfoDto rentalInfoDto,RentalLocationInfo locationInfo){
		rentalInfoDto.setLocationSerial(locationInfo.getLocationSerial());
		rentalInfoDto.setRentalType(locationInfo.getRentalType());
		rentalInfoDto.setArea(locationInfo.getArea());
		rentalInfoDto.setAcreage(locationInfo.getAcreage());
		rentalInfoDto.setCarPort(locationInfo.getCarPort());
		rentalInfoDto.setInsertUserId(locationInfo.getInsertUserId());	
		rentalInfoDto.setUpdateUserId(locationInfo.getUpdateUserId());		
		rentalInfoDto.setUpdateTime(locationInfo.getUpdateTime());
		rentalInfoDto.setAddress(locationInfo.getAddress());
		rentalInfoDto.setArea(locationInfo.getArea());
		rentalInfoDto.setHandleType(locationInfo.getHandleType());
		rentalInfoDto.setRemark(locationInfo.getRemark());
		rentalInfoDto.setAnnualTransactionVolume(locationInfo.getAnnualTransactionVolume());
		return rentalInfoDto;
	}
	
	/**
	 * 从dto向Entity赋值
	 * @param RentalInfoDto
	 * @return
	 */
	private RentalLocationInfo setEntityFromDto(RentalInfoDto rentalInfoDto){
		
		RentalLocationInfo locationInfo = new RentalLocationInfo();
		locationInfo.setId(rentalInfoDto.getId());
		locationInfo.setRowNo(rentalInfoDto.getRowNo());
		locationInfo.setLocationSerial(rentalInfoDto.getLocationSerial());
		locationInfo.setRentalType(rentalInfoDto.getRentalType());
		locationInfo.setRentalTypeName(rentalInfoDto.getRentalTypeName());
		locationInfo.setArea(rentalInfoDto.getArea());
		locationInfo.setRentalStatusName(rentalInfoDto.getStatusName());
		locationInfo.setAcreage(rentalInfoDto.getAcreage());
		locationInfo.setCarPort(rentalInfoDto.getCarPort());
		locationInfo.setInsertUserId(rentalInfoDto.getInsertUserId());
		locationInfo.setInsertUserName(rentalInfoDto.getInsertUserName());
		locationInfo.setInsertTime(rentalInfoDto.getInsertTime());
		locationInfo.setUpdateUserId(rentalInfoDto.getUpdateUserId());		
		locationInfo.setUpdateUserName(rentalInfoDto.getUpdateUserName());
		locationInfo.setUpdateTime(rentalInfoDto.getUpdateTime());
		locationInfo.setCssFlag(rentalInfoDto.getCssFlag());
		locationInfo.setAddress(rentalInfoDto.getAddress());
		locationInfo.setAnnualTransactionVolume(rentalInfoDto.getAnnualTransactionVolume());
		return locationInfo;
	}
	/**
	 * 从dto向Entity赋值
	 * @param RentalInfoDto
	 * @return
	 */
	private RentalLocationInfo setEntityFromDtoInfo(RentalInfoDto rentalInfoDto){		
		RentalLocationInfo locationInfo = new RentalLocationInfo();
		locationInfo.setRentalType(rentalInfoDto.getRentalType());
		locationInfo.setAcreage(rentalInfoDto.getAcreage());
		locationInfo.setCarPort(rentalInfoDto.getCarPort());
		locationInfo.setArea(rentalInfoDto.getArea());
		return locationInfo;
	}

	public void setRentalType(int rentalType) {
		this.rentalType = rentalType;
	}

	public int getRentalType() {
		return rentalType;
	}

	public void setAcreage(int acreage) {
		this.acreage = acreage;
	}

	public int getAcreage() {
		return acreage;
	}

	public void setCarPort(int carPort) {
		this.carPort = carPort;
	}

	public int getCarPort() {
		return carPort;
	}

	public void setInsertTime(Timestamp insertTime) {
		this.insertTime = insertTime;
	}

	public Timestamp getInsertTime() {
		return insertTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setUpdateUserId(int updateUserId) {
		this.updateUserId = updateUserId;
	}

	public int getUpdateUserId() {
		return updateUserId;
	}

	public void setLocationSerial(String locationSerial) {
		this.locationSerial = locationSerial;
	}

	public String getLocationSerial() {
		return locationSerial;
	}

	public void setRentalTypeName(String rentalTypeName) {
		this.rentalTypeName = rentalTypeName;
	}

	public String getRentalTypeName() {
		return rentalTypeName;
	}

	public void setRentalStatusName(String rentalStatusName) {
		this.rentalStatusName = rentalStatusName;
	}

	public String getRentalStatusName() {
		return rentalStatusName;
	}

	public void setInsertUserName(String insertUserName) {
		this.insertUserName = insertUserName;
	}

	public String getInsertUserName() {
		return insertUserName;
	}

	public void setUpdateUserName(String updateUserName) {
		this.updateUserName = updateUserName;
	}

	public String getUpdateUserName() {
		return updateUserName;
	}

	public void setRowNo(int rowNo) {
		this.rowNo = rowNo;
	}

	public int getRowNo() {
		return rowNo;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	public void setArea(int area) {
		this.area = area;
	}

	public int getArea() {
		return area;
	}

	public void setHandleType(int handleType) {
		this.handleType = handleType;
	}

	public int getHandleType() {
		return handleType;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}

	public void setHandleTypeName(String handleTypeName) {
		this.handleTypeName = handleTypeName;
	}

	public String getHandleTypeName() {
		return handleTypeName;
	}

	public void setHandleName(String handleName) {
		this.handleName = handleName;
	}

	public String getHandleName() {
		return handleName;
	}

	public void setHandleDate(String handleDate) {
		this.handleDate = handleDate;
	}

	public String getHandleDate() {
		return handleDate;
	}

	public String getAnnualTransactionVolume() {
		return annualTransactionVolume;
	}

	public void setAnnualTransactionVolume(String annualTransactionVolume) {
		this.annualTransactionVolume = annualTransactionVolume;
	}
	
}
